﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Configuration;
using StockStar.GatherData;
using Stockstar.Stock.Gather;

namespace EMNewStock
{
    class GatherData
    {
        public void DoWork()
        {
            initXGHY();
        }

        /// <summary>
        /// 初始化行业信息
        /// </summary>
        public static void initXGHY()
        {
            string _dir = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName;
            string _path = _dir + "\\" + "GoodsInd.txt";

            FileStream fs = File.Open(_path, FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.Default);
            string line = "";
            StringBuilder sb = new StringBuilder();
            while ((line = reader.ReadLine()) != null)
            {
                string[] arg = line.Split(new char[] { ',' });
                if (arg.Length == 2)
                {
                    sb.AppendFormat("update XG_Detail set sshy = '{1}' where gpdm='{0}'\r\n", arg[0].Trim(), arg[1].Trim());
                }
            }

            reader.Close();
            fs.Close();
            fs.Dispose();

            _path = _dir + "\\" + "GoodsIndre.txt";
            using (StreamWriter sw = new StreamWriter(_path, false, Encoding.Default))
            {
                sw.Write(sb.ToString());
                sw.Close();
            }

            using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sb.ToString(), null);
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                    }
                }
            }
        }

        /// <summary>
        /// 生成数据文件
        /// </summary>
        public static void createXgIndex()
        {
            string _urlLessDay = "http://datacenter.emoney.cn/stock/NewstockListFormat.ashx?action=LessDay";
            string _urlLessEqual = "http://datacenter.emoney.cn/stock/NewstockListFormat.ashx?action=LessEqual";
            string _urlThisDay = "http://datacenter.emoney.cn/stock/NewstockListFormat.ashx?action=ThisDay";
            string _urlGreaterDay = "http://datacenter.emoney.cn/stock/NewstockListFormat.ashx?action=GreaterDay";
            string _dirLessDay = "D:\\down\\xg\\NewstockListFormatLessDay.txt";
            string _dirLessEqual = "D:\\down\\xg\\NewstockListFormatLessEqual.txt";
            string _dirThisDay = "D:\\down\\xg\\NewstockListFormatThisDay.txt";
            string _dirGreaterDay = "D:\\down\\xg\\NewstockListFormatGreaterDay.txt";

            string _ContentLessDay = OperMethod.GetPageByUrl(_urlLessDay, "gb2312");
            string _ContentLessEqual = OperMethod.GetPageByUrl(_urlLessEqual, "gb2312");
            string _ContentThisDay = OperMethod.GetPageByUrl(_urlThisDay, "gb2312");
            string _ContentGreaterDay = OperMethod.GetPageByUrl(_urlGreaterDay, "gb2312");

            //小于今天，已上市
            using (StreamWriter sw = new StreamWriter(_dirLessDay, false, Encoding.GetEncoding(936)))
            {
                sw.Write(_ContentLessDay);
                sw.Close();
            }
            //小于等于今天，已上市
            using (StreamWriter sw = new StreamWriter(_dirLessEqual, false, Encoding.GetEncoding(936)))
            {
                sw.Write(_ContentLessEqual);
                sw.Close();
            }
            //等于今天，今天上市
            using (StreamWriter sw = new StreamWriter(_dirThisDay, false, Encoding.GetEncoding(936)))
            {
                sw.Write(_ContentThisDay);
                sw.Close();
            }
            //大于今天，未上市
            using (StreamWriter sw = new StreamWriter(_dirGreaterDay, false, Encoding.GetEncoding(936)))
            {
                sw.Write(_ContentGreaterDay);
                sw.Close();
            }
        }
    }
}
